package com.library.repository.impl;/*
 *@author lzg
 *@data 2020/11/15
 */

import com.library.entity.Reader;
import com.library.repository.ReaderRepository;
import com.library.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReaderRepositoryImpl implements ReaderRepository {


    @Override
    public Reader login(String username, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from reader where username=? and password=?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Reader reader =null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setObject(1,username);
            statement.setObject(2,password);
            resultSet = statement.executeQuery();
            //查询出结果后封装到对象中
            if (resultSet.next()){
                reader = new Reader(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4),resultSet.getString(5),resultSet.getString(6),resultSet.getString(7));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection,statement,resultSet);
        }

        return reader;
    }
}
